#include<vector>
#include<iostream>
#include<string>
#include<string.h>
#include<sstream>
#include"cpp-httplib/httplib.h"
#include"util.hpp"
#include "daemonize.hpp"

const std::string root_path="./wwwroot";
const char* DebugPath ="./log/log.txt";
const char* ErrorPath ="./log/err.txt";

int main()
{
    // 日志重定向
    Redirect red(DebugPath,ErrorPath);
    // if(!daemonize())
    // {
    //     LOGERR("daemonize error!");
    //     exit(-1);
    // }
    httplib::Server svr;
    svr.set_base_dir(root_path.c_str());
    // 获得Manager单例
    Manager *manager =Manager::GetInstance();
    manager->init();

    //处理client登录请求
    svr.Get("/log",[](const httplib::Request &req,httplib::Response &rep){
        if(!req.has_param("word"))
        {
            return;
        }
        int ret = LogUtil::Log(req.params);
        {
            if(ret == SUCCESS)
            {
                rep.set_content("SUCCESS","txt/plain");
            }
            else if(ret == PSW_WRONG)
            {
                rep.set_content("PSW_WRONG","txt/plain");
            }
            else if(ret == NOEXIST)
            {
                rep.set_content("NOEXIST","txt/plain");
            }
            else
            {
                rep.status =500;
                rep.set_content("FATAL","txt/plain");
            }
        }
    });

    svr.Get("/GetUserData",[](const httplib::Request &req,httplib::Response &rep){
        std::string jsonString;
        DataUtil::GetUserData(jsonString);
        rep.set_content(jsonString,"application/json");
    });

    svr.Get("/DelUserData",[](const httplib::Request &req,httplib::Response &rep){
        int ret = DataUtil::DelUserData(req.params);
        Json::Value root;
        if(ret == SUCCESS)
        {
            root["status"] = "SUCCESS";
        }
        else
        {
            root["status"] = "ERROR";
        }
        Json::FastWriter writer;
        const std::string jsonString = writer.write(root);
        rep.set_content(jsonString,"application/json");
    });

    svr.Post("/UpUserData",[](const httplib::Request &req,httplib::Response &rep){
        int ret = DataUtil::UpUserData(req.params);
        Json::Value root;
        if(ret == SQLERROR)
        {
            root["status"] ="ERROR";
            LOGERR(" UPUserData error!");
        }
        else
        {
            root["status"] ="SUCCESS";
        }
        Json::FastWriter writer;
        const std::string jsonString = writer.write(root);
        rep.set_content(jsonString,"application/json");
    });

    svr.Post("/AddUserData",[](const httplib::Request &req,httplib::Response &rep){
        int ret = DataUtil::AddUserData(req.params);
        Json::Value root;
        if( ret == SUCCESS)
        {
            root["status"] = "SUCCESS";
        }
        else{
            root["status"] = "ERROR";
        }
        Json::FastWriter writer;
        const std::string jsonString = writer.write(root);
        rep.set_content(jsonString,"application/json");
    });

    svr.Get("/GetPayList",[](const httplib::Request &req,httplib::Response &rep){
        std::string jsonString;
        DataUtil::GetPayList(jsonString);
        rep.set_content(jsonString,"application/json");
    });

    svr.Post("/AddPayData",[](const httplib::Request &req,httplib::Response &rep){
        int ret =DataUtil::AddPayData(req.params);
        Json::Value root;
        if(ret == SUCCESS)
        {
            root["status"] = "SUCCESS";
        }
        else{
            root["status"] = "ERROR" ;
        }
        Json::FastWriter writer;
        const std::string jsonString = writer.write(root);
        rep.set_content(jsonString,"application/json");
    });

    svr.Get("/DelPayData",[](const httplib::Request &req,httplib::Response &rep){
        int ret = DataUtil::DelPayData(req.params);
        Json::Value root;
        if(ret == SUCCESS)
        {
            root["status"] = "SUCCESS";
        }
        else
        {
            root["status"] = "ERROR";
        }
        Json::FastWriter writer;
        const std::string jsonString = writer.write(root);
        rep.set_content(jsonString,"application/json");
    });

    svr.Get("/GetRepairList",[](const httplib::Request &req,httplib::Response &rep){
        std::string jsonString;
        DataUtil::GetRepairList(jsonString);
        rep.set_content(jsonString,"application/json");
    });

    svr.Get("/DelRepairData",[](const httplib::Request &req,httplib::Response &rep){
        int ret = DataUtil::DelRepairData(req.params);
        Json::Value root;
        if(ret == SUCCESS)
        {
            root["status"] = "SUCCESS";
        }
        else
        {
            root["status"] = "ERROR";
        }
        Json::FastWriter writer;
        const std::string jsonString = writer.write(root);
        rep.set_content(jsonString,"application/json");
    });

    svr.Post("/AddRepairData",[](const httplib::Request &req,httplib::Response &rep){
        int ret =DataUtil::AddRepairData(req.params);
        Json::Value root;
        if(ret == SUCCESS)
        {
            root["status"] = "SUCCESS";
        }
        else{
            root["status"] = "ERROR" ;
        }
        Json::FastWriter writer;
        const std::string jsonString = writer.write(root);
        rep.set_content(jsonString,"application/json");
    });

    svr.Get("/PlanList",[](const httplib::Request &req,httplib::Response &rep){
        std::string jsonString;
        DataUtil::PlanList(jsonString);
        rep.set_content(jsonString,"application/json");
    });
    svr.Post("/Login",[](const httplib::Request &req,httplib::Response &rep){
        int ret = DataUtil::AddUser(req.params);
        Json::Value root;
        if(ret == SUCCESS)
        {
            root["status"] = "SUCCESS";
        }
        else{
            root["status"] = "EXIST" ;
        }
        Json::FastWriter writer;
        const std::string jsonString = writer.write(root);
        rep.set_content(jsonString,"application/json");
    });

    svr.Get("/GetMedicalList",[](const httplib::Request &req,httplib::Response &rep){
        std::string jsonString;
        DataUtil::GetMedicalList(jsonString);
        rep.set_content(jsonString,"application/json");
    });

    svr.Post("/AddMedicalData",[](const httplib::Request &req,httplib::Response &rep){
        int ret =DataUtil::AddMedicalData(req.params);
        Json::Value root;
        if(ret == SUCCESS)
        {
            root["status"] = "SUCCESS";
        }
        else{
            root["status"] = "ERROR" ;
        }
        Json::FastWriter writer;
        const std::string jsonString = writer.write(root);
        rep.set_content(jsonString,"application/json");
    });

    svr.Get("/DelMedicalData",[](const httplib::Request &req,httplib::Response &rep){
        int ret = DataUtil::DelMedicalData(req.params);
        Json::Value root;
        if(ret == SUCCESS)
        {
            root["status"] = "SUCCESS";
        }
        else
        {
            root["status"] = "ERROR";
        }
        Json::FastWriter writer;
        const std::string jsonString = writer.write(root);
        rep.set_content(jsonString,"application/json");
    });

    svr.Get("/api/ping", [](const httplib::Request& req, httplib::Response& rep) {
    rep.set_content("pong", "text/plain");

    });
    svr.Post("/api/send-message", [](const httplib::Request& req, httplib::Response& rep) {
    // std::string message = req.get_param_value("message");
    std::string Jsonstring;
    Json::Value root;
    root["rep"]="hello";
    Json::FastWriter writer;
    Jsonstring=writer.write(root);
    rep.set_content(Jsonstring, "application/json");
    });
    svr.listen("0.0.0.0",8081);
}

